
checkLogin();

getSliders();

mui.init({
    swipeBack: false,
});
//侧滑容器父节点
var offCanvasWrapper = mui('#offCanvasWrapper');

// 轮播图
function getSliders() {
    var success = function (res) {
        var slider = res.data;
        /*slider.waterbill_list = null;*/
        if(slider.waterbill_list !=undefined && slider.waterbill_list != null && slider.waterbill_list!='' && slider.waterbill_list.length > 0){
            $('#offCanvasContentScroll').removeClass('none');
            $('.no_ticket').addClass('none');
            $('.sure-submit').removeClass('none');

            template.defaults.imports.slider_nums = slider.waterbill_list.length - 1;
            var html = template('slider_img',slider);
            $('#slider_img_content').append(html);
            mui('#offCanvasContentScroll').scroll();
            var gallery = mui('.mui-slider');
            gallery.slider({
                interval:0//自动轮播周期，若为0则不自动播放，默认为0；
            });

            //最大可增加数 默认为轮播图的第一个对应参数
            max_num = slider.waterbill_list[0].rest_num;
            $('#tickt_id').val(slider.waterbill_list[0].id);

            //监听轮播图的切换
            document.querySelector('.mui-slider').addEventListener('slide', function(event) {
                //将当前第几张图片 信息 放入hidden 中
                var slideNumbers = event.detail.slideNumber;
                $('#slideNumbers').val(slideNumbers);

                //根据具体业务，动态获得 对应数据
                max_num = slider.waterbill_list[event.detail.slideNumber].rest_num;
                document.getElementById('max-button').removeAttribute("disabled");
                $('#tickt_id').val(slider.waterbill_list[event.detail.slideNumber].id);
                //监听轮播切换时 input中的数字是否超过当前余票多大值
                var num = $('#mui-numbox-input').val();
                if(num >= max_num){
                    if(max_num <= 0){
                        $('#mui-numbox-input').val(1);
                    }else {
                        $('#mui-numbox-input').val(max_num);
                    }
                    document.getElementById('max-button').setAttribute("disabled","disabled");
                    document.getElementById('min-button').removeAttribute("disabled");

                }
            });

            //绑定加 事件
            $('#max-button').on('tap',function () {
                console.log(max_num)
                //获取 输入框的数据
                var num = $('#mui-numbox-input').val();
                if(num >= max_num){
                    document.getElementById('max-button').setAttribute("disabled","disabled");
                    document.getElementById('min-button').removeAttribute("disabled");
                    if(max_num <= 0){
                        $('#mui-numbox-input').val(1);
                    }else {
                        $('#mui-numbox-input').val(max_num);
                    }
                }
            })

        }else {
            $('#offCanvasContentScroll').addClass('none');
            $('.no_ticket').removeClass('none');
            $('.sure-submit').addClass('none');
        }

        //无水票时 点击跳转到水票列表
        $('.toTickList').on('tap',function () {
            redirect("water-ticket.html");
        })

        $("#mui-numbox-input").bind('input porpertychange',function(){
            var num = $('#mui-numbox-input').val();
            if(num >= max_num){
                if(max_num <= 0){
                    $('#mui-numbox-input').val(1);
                }else {
                    $('#mui-numbox-input').val(max_num);
                }
                document.getElementById('max-button').setAttribute("disabled","disabled");
                document.getElementById('min-button').removeAttribute("disabled");
            }
        });


        $('.left_img').on('tap',function () {
            var slideNumbers = $('#slideNumbers').val();
            if(slideNumbers <= 0){
                return;
            }
            //获得slider插件对象
            var gallery = mui('.mui-slider');
            gallery.slider().gotoItem(parseInt(slideNumbers)-1);//跳转到第index张图片，index从0开始；
        })

        $('.right_img').on('tap',function () {
            var slideNumbers = $('#slideNumbers').val();
            if(slideNumbers >= slider.waterbill_list.length-1){
                return;
            }
            //获得slider插件对象
            var gallery = mui('.mui-slider');
            gallery.slider().gotoItem(parseInt(slideNumbers)+1);//跳转到第index张图片，index从0开始；
        })

        /*//点击导航栏订水项，如果有水票不跳转，无水票跳转到主页
        $('.sure-submit').on('tap','#water_take',function () {
            if(isEmpty(slider.waterbill_list) || slider.waterbill_list.length <=0){
                redirect('take-water.html');
            }else {
                redirect('member-ticket.html');
            }
        })*/
        
        //若水票用完  点击购买
        $('.toBuy').on('tap',function () {
            redirect('water-ticket.html');
        })

    };
    getData(member_waterbill,comParams(),success);
}

//请求默认地址 以及 地址列表
var success = function (res) {

    var info = res.data;
    var html = template('address',info);
    //默认地址
    var address = "";
    var addr = "";
    if(info.address_list != undefined && info.address_list != null && info.address_list != '' && info.address_list.length > 0){
        $('.show_noaddress').removeClass('none');
        for(var i=0;i<info.address_list.length;i++){
            if(info.address_list[i].is_default != undefined
                &&info.address_list[i].is_default != null
                && info.address_list[i].is_default !=''
                && info.address_list[i].is_default == 1)
            {
                address = info.address_list[i];
                if(address == undefined || address == null || address == ''){
                    html = '<div class="to_addAddress" style="height: 1.5rem;line-height: 1.5rem;"><img style="width: 1.5rem;height: 1.5rem;float: left" src="image/add.png"><span style="color: rgba(0,0,0,.2);font-size: 0.9rem;margin-left: 0.5rem">暂无地址，请点击添加地址</span><img style="width: 1rem;height: 1rem;float: right;margin-top: 0.3rem" src="image/right.png"></div>'
                }else {
                    addr = address.area_info+" "+address.position_address+address.address;
                }
                continue;
            }
        }
    }else {
        $('.show_noaddress').css('display','block');
        html = '<div class="to_addAddress" style="height: 1.5rem;line-height: 1.5rem;"><img style="width: 1.5rem;height: 1.5rem;float: left" src="image/add.png"><span style="color: rgba(0,0,0,.7);font-size: 0.9rem;margin-left: 0.5rem">暂无地址，请点击添加地址</span><img style="width: 1rem;height: 1rem;float: right;margin-top: 0.3rem" src="image/right.png"></div>'
    }

    $('#address_content').append(html);
    $('#address_detail').text(addr);
    $('.no_address').bind('click',function () {
        redirect("add-address.html?index=1");
    })

    //地址列表
    var html = template('address_list',info);
    $('#address_list_content').append(html);
    $('.choosed').addClass('none');
    if(info.address_list != undefined && info.address_list != null && info.address_list != ''){
        for(var i=0;i<info.address_list.length;i++){
            if(info.address_list[i].is_default != undefined
                &&info.address_list[i].is_default != null
                && info.address_list[i].is_default !=''
                && info.address_list[i].is_default == 1)
            {
                $('.choosed'+info.address_list[i].id).removeClass('none');
            }
        }
    }

    //监听如果遮罩层打开，就关闭
    if(mui('.mui-off-canvas-wrap').offCanvas().isShown())
    {
        mui('.mui-off-canvas-wrap').offCanvas().close();
    }

    //监听遮罩层的点击事件
    mui('.list_ocntent').on('tap', '.ad_list', function () {
        var sPrents = $(this).parents(".list_ocntent");
        var address_id = sPrents.attr('id');
        console.log(address_id)
        if(info.address_list != undefined && info.address_list != null && info.address_list != ''){
            for(var i=0;i<info.address_list.length;i++) {
                address = info.address_list[i];
                //判断 如果是已被选中的，则返回
                if(address.id == address_id && address.is_default == 1){
                    $('.choosed'+address_id).removeClass('none');
                    return;
                }
                info.address_list[i].is_default = 0;
                $('.choosed').addClass('none');
                if(info.address_list[i].id == address_id){
                    $('.choosed'+address_id).removeClass('none');
                    info.address_list[i].is_default = 1;
                    var address_detail = info.address_list[i];
                    $('.about-name-new').text(address_detail.name);
                    $('.about_mobile').text(address_detail.mobile);
                    var addr = address_detail.area_info+" "+address_detail.position_address+address_detail.address;
                    $('#address_detail').text(addr);
                    //设置默认地址
                    postData(address_set_default,combineParams
                    ({id:address.id,name:address.name,mobile:address.mobile,address:address.address,area_info:address.area_info}));
                    break;
                }
            }
        }
    });

    //显示或隐藏遮罩层
    $('.nctouch-cart-add-default').on('tap',function () {
        if(!mui('.mui-off-canvas-wrap').offCanvas().isShown())
        {
            $('.mui-off-canvas-left').css('z-index',2);
            mui('.mui-off-canvas-wrap').offCanvas().show();
        }else {
            mui('.mui-off-canvas-wrap').offCanvas().close();
        }
    })

    $('.mui-off-canvas-right').on('tap',function () {
        mui('.mui-off-canvas-wrap').offCanvas().close();
        /*$('.zhezhao').addClass('none');*/
    })

    // 禁止冒泡，禁止跟手势滑动：
    document.getElementsByClassName('mui-inner-wrap')[0].addEventListener('drag', function(event) {
        event.stopPropagation();
    });


    //绑定减去事件
    $('#min-button').on('tap',function () {
        var num = $('#mui-numbox-input').val();
        if(num =1){
            $('#mui-numbox-input').text(1);
        }
        if(num > 3){
            return;
        }
        console.log(11234)
    })

    //无水票时 点击跳转到水票列表
    $('.toTickList').on('tap',function () {
        redirect("water-ticket.html");
    })


    //点击确定订水 调用方法
    document.getElementById("button").addEventListener('tap', function() {
        /*//判断是否填写
        var mobile = getCache(login_key).mobile;
        if(isEmpty(mobile)){
            malert('未绑定账户资料，立即去绑定','提示',function (e) {
                redirect("member-info.html?ref=2");
            })
        }else {
            take_water();
        }*/
        take_water();

    });
    
    //订水
    function take_water() {
        //获取订的票数
        var buy_num = $('#mui-numbox-input').val();
        //弹框
        var btnArray = ['取消', '确定'];
        mui.confirm('你确定要购买'+buy_num+'桶水？', '提示', btnArray, function(e) {
            if (e.index == 1) {
                //获取要传入的参数
                var id = $('#tickt_id').val();
                var num = buy_num;
                var buyer_name = address.name;
                var mobile = address.mobile;
                var buyer_address = address.area_info +" "+address.position_address+address.address;
                var description = $('#remark-input').val();

                var success = function (res) {
                    console.log(res);
                    //表示是下单的成功页面
                    setTimeout(function() {
                        locationReplace("success.html?type=3");
                    }, 2000);
                }

                //调用一键订水购买接口
                postData(order_add_bill_order,combineParams({
                    id:id,
                    num:num,
                    buyer_name:buyer_name,
                    mobile:mobile,
                    buyer_address:buyer_address,
                    description:description}),success);

            }
        })
    }
    
    //点击添加地址
    $('.to_addAddress').on('tap',function () {
        redirect("add-address.html?ref=2");
       /*var mobile = getCache(login_key).mobile;
       if(isEmpty(mobile)){
           redirect("member-info.html?ref=2");
       }else {
           redirect("add-address.html?ref=2");
       }*/
    })

    /*var login = getCache(login_key);
    var sid = login.sid;*/

    //获取原始窗口的高度
    var originalHeight=document.documentElement.clientHeight || document.body.clientHeight;
    /*window.onresize = function (ev) {
       /!* console.log(ev)*!/
        var  resizeHeight=document.documentElement.clientHeight || document.body.clientHeight;
        if(resizeHeight*1<originalHeight*1){ //resizeHeight<originalHeight证明窗口被挤压了
            /!*if(sid == 1){
                mui.alert("yasuo");
            }*!/

            /!*$('.goods-carts').css('display','none');*!/
            $('.sure-submit').css('display','none');
            /!*$('.remark-input').addClass('pos_fixed');*!/
            /!*$('.bac_rgba').removeClass('none');*!/
            $('#offCanvasContentScroll').css('margin-bottom',0);

        }else{
            /!*if(sid == 1){
                mui.alert("zhankai");
            }*!/
            /!*$('.goods-carts').css('display','block');*!/
            $('.sure-submit').css('display','block');
            /!*$('.remark-input').removeClass('pos_fixed');*!/
            /!*$('.bac_rgba').addClass('none');*!/
            $('#offCanvasContentScroll').css('margin-bottom','4rem');
        }

    }*/
    /*$('#remark-input').bind('click',function () {
        var  resizeHeight=document.documentElement.clientHeight || document.body.clientHeight;
        if(resizeHeight*1<originalHeight*1){ //resizeHeight<originalHeight证明窗口被挤压了
            /!*if(sid == 1){
                mui.alert("yasuo");
            }*!/

            $('.sure-submit').css('display','none');
            $('.remark-input').addClass('pos_fixed');
            $('.bac_rgba').removeClass('none');

        }else{
            /!*if(sid == 1){
                mui.alert("zhankai");
            }*!/

            $('.sure-submit').css('display','block');
            $('.remark-input').removeClass('pos_fixed');
            $('.bac_rgba').addClass('none');
        }
    })*/

    $('.bac_rgba').on('tap',function () {
        $('.remark-input').removeClass('pos_fixed');
        $('.bac_rgba').addClass('none');
        $('.sure-submit').css('display','block');
        document.activeElement.blur();//关闭软键盘
    })

}
getData(address_list,comParams(),success);

//点击跳转到我的页面
$('.goods-carts').on('tap',function () {
    redirect('member.html');
})

//获取营业时间
var success = function (res) {
    var info = res.data.info;
    console.log(info)
    var html = template('time',info);
    $('#time_content').append(html);

}
getData(shop_info,comParams(),success);



/*
//地址列表

var success = function (res) {
    var info = res.data;
    var html = template('address_list',info);
    $('#address_list_content').append(html);
    //监听遮罩层的点击事件
    mui('.list_ocntent').on('tap', '.backdrop', function () {
        var sPrents = $(this).parents(".list_ocntent");
        var address_detail = sPrents.attr('id');
        console.log(address_detail)
        if(mui('.mui-off-canvas-wrap').offCanvas().isShown())
        {
            mui('.mui-off-canvas-wrap').offCanvas().close();
            $('.zhezhao').addClass('none');
        }

    });


//显示或隐藏遮罩层
    $('.mui-action-menu').on('tap',function () {
        if(!mui('.mui-off-canvas-wrap').offCanvas().isShown())
        {
            $('.mui-off-canvas-left').css('z-index',2);
            mui('.mui-off-canvas-wrap').offCanvas().show();
            $('.zhezhao').removeClass('none');
        }else {
            mui('.mui-off-canvas-wrap').offCanvas().close();
            $('.zhezhao').addClass('none');
        }
    })
}
getData(address_list,comParams(),success);*/
